package com.food.admin.controller;


import com.food.admin.dto.AccountDto;
import com.food.admin.dto.AccountListDto;
import com.food.admin.entity.Account;
import com.food.admin.exception.MyParameterException;
import com.food.admin.form.*;
import com.food.admin.model.Result;
import com.food.admin.service.AccountService;
import com.food.admin.util.JwtUtil;
import com.food.admin.util.MyPage;
import com.food.admin.util.RedisUtil;
import com.food.admin.util.WebUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 帅得一
 * @since 2023年06月13日
 */
@RestController
@RequestMapping("/account")
@Api(tags = "管理员账号接口")
public class AccountController {

    @Autowired
    private AccountService accountService;
    @Autowired
    private RedisUtil redisUtil;
    @PostMapping("/login")
    @ApiOperation("登录")
    public Result login(@RequestBody @Valid AccountLoginForm form, BindingResult br) throws Exception{
        if (br.hasErrors()) {
            List<ObjectError> allErrors = br.getAllErrors();
            for (ObjectError allError : allErrors) {
                //获取打印提示信息
                System.out.println(allError.getDefaultMessage());
            }
            throw new MyParameterException(500, "参数校验失败");
        }
        AccountDto accountDto = accountService.login(form);
        //登录成功后签发jwt
        HashMap<String, Object> body = new HashMap<>();
        body.put("sub",accountDto.getAccountId());
        Integer min=30;
        //签发短token
        String accessToken = JwtUtil.createToken(body, min);
        //签发长token
        String refreshToken = JwtUtil.createToken(body, 8*60);
        //将长token保存到redis中
        redisUtil.setEx(accessToken,refreshToken,8*60*60);
        //将token保存到响应头中 响应给客户端
        WebUtil.getResponse().setHeader("accessToken",accessToken);
        return Result.success("200","登录成功",accountDto);
   }

   @PostMapping("/insert")
   @ApiOperation("新增管理员")
   public Result insert(@RequestBody @Valid AccountInsertForm form,BindingResult br) throws Exception{
       if (br.hasErrors()) {
           List<ObjectError> allErrors = br.getAllErrors();
           for (ObjectError allError : allErrors) {
               //获取打印提示信息
               System.out.println(allError.getDefaultMessage());
           }
           throw new MyParameterException(500, "参数校验失败");
       }
        Account account = accountService.insert(form);
        return Result.success("200","添加管理员成功",account);
   }

    @GetMapping("/list")
    @ApiOperation("查询所有管理账号")
    public Result list(AccountListPageForm form) throws Exception{
        List<AccountListDto> page = accountService.listAll(form);
        return Result.success("200","查询所有管理员信息成功",page);
    }

   @PostMapping("/updateState")
   @ApiOperation("修改管理员账号状态")
   public Result updateState(@RequestBody @Valid AccountUpdateStateForm form,BindingResult br) throws Exception{
       if (br.hasErrors()) {
           List<ObjectError> allErrors = br.getAllErrors();
           for (ObjectError allError : allErrors) {
               //获取打印提示信息
               System.out.println(allError.getDefaultMessage());
           }
           throw new MyParameterException(500, "参数校验失败");
       }
       Account account = accountService.updateState(form);
       return Result.success("200","修改管理员账号状态成功",null);
   }

    @PostMapping("/updatePassword")
    @ApiOperation("修改密码")
    public Result updatePassword(@RequestBody AccountUpdatePasswordForm form)throws Exception{
        accountService.updatePassword(form);
        return Result.success("200","修改密码成功",null);
    }

    @PostMapping("/updateNickname")
    @ApiOperation("修改昵称")
    public Result updateNickname(@RequestBody AccountUpdateNicknameForm form)throws Exception{
        accountService.updateNickname(form);
        return Result.success("200","修改昵称成功",null);
    }

    @PostMapping("/outLogin")
    @ApiOperation("退出登录")
    public Result outLogin()throws Exception{
        accountService.outLogin();
        return Result.success("200","退出登录成功",null);
    }


    @PostMapping("/authorization")
    @ApiOperation("授权")
    public Result authorization(@Valid @RequestBody AccountAuthorizationForm form,BindingResult br){
        if (br.hasErrors()) {
            List<ObjectError> allErrors = br.getAllErrors();
            for (ObjectError allError : allErrors) {
                //获取打印提示信息
                System.out.println(allError.getDefaultMessage());
            }
            throw new MyParameterException(500, "参数校验失败");
        }
        accountService.authorization(form);
        return Result.success("200","授权成功",null);
    }
}

